package com.atom.process.cnmuseum.cj.delegates;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.IdentityLinkType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.atom.common.VariablesConstant;
import com.atom.common.activiti.RunningTaskHandler;
import com.atom.process.cnmuseum.cj.dao.process.CjDbrwProcessDao;
import com.atom.process.cnmuseum.cj.dao.process.CjGcxxProcessDao;
import com.atom.process.cnmuseum.cj.dao.process.CjProcessDao;
import com.atom.process.cnmuseum.cj.dao.process.CjWhrlxxxProcessDao;
import com.atom.process.cnmuseum.cj.dao.process.CjWhsxProcessDao;
import com.atom.process.cnmuseum.cj.dao.process.CjXzxxProcessDao;
import com.atom.process.cnmuseum.cj.dao.process.CjYxxxProcessDao;
import com.atom.process.cnmuseum.cj.dao.task.CjDbrwTaskDao;
import com.atom.process.cnmuseum.cj.dao.task.CjGcxxTaskDao;
import com.atom.process.cnmuseum.cj.dao.task.CjTaskDao;
import com.atom.process.cnmuseum.cj.dao.task.CjWhrlxxxTaskDao;
import com.atom.process.cnmuseum.cj.dao.task.CjWhsxTaskDao;
import com.atom.process.cnmuseum.cj.dao.task.CjXzxxTaskDao;
import com.atom.process.cnmuseum.cj.dao.task.CjYxxxTaskDao;
import com.atom.process.cnmuseum.cj.entity.process.CjDbrwProcessDO;
import com.atom.process.cnmuseum.cj.entity.process.CjGcxxProcessDO;
import com.atom.process.cnmuseum.cj.entity.process.CjProcessDO;
import com.atom.process.cnmuseum.cj.entity.process.CjWhrlxxxProcessDO;
import com.atom.process.cnmuseum.cj.entity.process.CjWhsxProcessDO;
import com.atom.process.cnmuseum.cj.entity.process.CjXzxxProcessDO;
import com.atom.process.cnmuseum.cj.entity.process.CjYxxxProcessDO;
import com.atom.process.cnmuseum.cj.entity.task.CjDbrwTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjGcxxTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjWhrlxxxTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjWhsxTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjXzxxTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjYxxxTaskDO;
import com.atom.process.cnmuseum.cj.tool.TaskDefinitions;

@Component("CollectCreateListener")
public class CollectCreateListener implements Serializable {

    @Resource
    RuntimeService runtimeService;
    @Resource
    RunningTaskHandler runningTaskHandler;

    @Resource
    CjGcxxProcessDao cjGcxxProcessDao;
    @Resource
    CjGcxxTaskDao cjGcxxTaskDao;

    @Resource
    CjProcessDao cjProcessDao;
    @Resource
    CjTaskDao cjTaskDao;

    @Resource
    CjXzxxProcessDao cjXzxxProcessDao;
    @Resource
    CjXzxxTaskDao cjXzxxTaskDao;
    
    @Resource
    CjYxxxProcessDao cjYxxxProcessDao;
    @Resource
    CjYxxxTaskDao cjYxxxTaskDao;
    

    @Resource
    CjWhrlxxxProcessDao cjWhrlxxxProcessDao;
    @Resource
    CjWhrlxxxTaskDao cjWhrlxxxTaskDao;
    

    @Resource
    CjDbrwProcessDao cjDbrwProcessDao;
    @Resource
    CjDbrwTaskDao cjDbrwTaskDao;
    

    @Resource
    CjWhsxProcessDao cjWhsxProcessDao;
    @Resource
    CjWhsxTaskDao cjWhsxTaskDao;
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    public void execute(DelegateTask task) {

        String mainProcessInstanceId = (String) task.getVariable(VariablesConstant.MAIN_PROCESS_INSTANCE_ID);
        // 开始流程不用创建
        if (TaskDefinitions.采集.equal(task.getTaskDefinitionKey())
                && mainProcessInstanceId == null) {
            return;
        }
        // 结束流程不用创建
        if (TaskDefinitions.资源审核.equal(task.getTaskDefinitionKey())) {
//            CjGcxxTaskDO lasttask = cjGcxxTaskDao.findLastByProcessId(mainProcessInstanceId);
//            String next = lasttask.getNext();
//            if ("cj".equals(next)) {// 采集审核后不生成新流程，直接归档
//                return;
//            }
        }
        // 把PDO的值赋给ADO
        PDO2ADO(mainProcessInstanceId, task.getId());


        // 写平台待办表
        Map<String, Object> variables = new HashMap<String, Object>();
        // TASK SCOPE VARS
        Map<String, Object> task_var = task.getVariables();
        variables.putAll(task_var);
        // EXECUTION SCOPE VARS
        Map<String, Object> exe_var = runtimeService.getVariables(mainProcessInstanceId);
        variables.putAll(exe_var);
        if (task.getAssignee() != null) {
            runningTaskHandler.insert(task, variables, task.getAssignee());
        } else {
            Set<IdentityLink> links = task.getCandidates();
            for (IdentityLink identityLink : links) {
                if (StringUtils.equals(IdentityLinkType.CANDIDATE, identityLink.getType())) {
                    runningTaskHandler.insert(task, variables, identityLink.getUserId());
                }
            }
        }
    }

    private void PDO2ADO(String mainProcessInstanceId, String taskId) {

        CjTaskDO ado = new CjTaskDO();
        CjProcessDO pdo = cjProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        BeanUtils.copyProperties(pdo, ado, "id");
        ado.setTaskId(taskId);
        cjTaskDao.save(ado);

        CjGcxxTaskDO gcado = new CjGcxxTaskDO();
        CjGcxxProcessDO gcpdo = cjGcxxProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        BeanUtils.copyProperties(gcpdo, gcado, "id");
        gcado.setTaskId(taskId);
        cjGcxxTaskDao.save(gcado);

        CjXzxxTaskDO xzado = new CjXzxxTaskDO();
        CjXzxxProcessDO xzpdo = cjXzxxProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        BeanUtils.copyProperties(xzpdo, xzado, "id");
        xzado.setTaskId(taskId);
        cjXzxxTaskDao.save(xzado);


        CjYxxxTaskDO yxado = new CjYxxxTaskDO();
        CjYxxxProcessDO yxpdo = cjYxxxProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        BeanUtils.copyProperties(yxpdo, yxado, "id");
        yxado.setTaskId(taskId);
        cjYxxxTaskDao.save(yxado);
        
        
        CjWhrlxxxTaskDO whrlxxxado = new CjWhrlxxxTaskDO();
        CjWhrlxxxProcessDO whrlxxxpdo = cjWhrlxxxProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        BeanUtils.copyProperties(whrlxxxpdo, whrlxxxado, "id");
        whrlxxxado.setTaskId(taskId);
        cjWhrlxxxTaskDao.save(whrlxxxado);
        
        
        List<CjDbrwProcessDO> dbrwpdo_list = cjDbrwProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        for(CjDbrwProcessDO dbrwpdo:dbrwpdo_list){        	
        	CjDbrwTaskDO dbrwado = new CjDbrwTaskDO();
        	BeanUtils.copyProperties(dbrwpdo, dbrwado, "id");
        	dbrwado.setTaskId(taskId);
        	cjDbrwTaskDao.save(dbrwado);
        }

        List<CjWhsxProcessDO> whsxpdo_list = cjWhsxProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        for(CjWhsxProcessDO whsxpdo:whsxpdo_list){    
        	CjWhsxTaskDO whsxado = new CjWhsxTaskDO();
        	BeanUtils.copyProperties(whsxpdo, whsxado, "id");
        	whsxado.setTaskId(taskId);
        	cjWhsxTaskDao.save(whsxado);
        }
    }
}
